package com.curry.android.util;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.R;

/**
 * 进度条
 *
 * @author Administrator
 */
public class DialogHelper {

    /**
     * 得到自定义的progressDialog
     *
     * @param context
     * @return
     */private static DialogHelper dialogHelper;
    private ProgressDialog progressDialog;

    public static DialogHelper getInstance() {
        if (dialogHelper == null) {
            dialogHelper = new DialogHelper();
        }
        return dialogHelper;
    }

    public Dialog createLoadingDialog(Context context) {
        return createLoadingDialog(context, "");
    }

    @Deprecated
    public Dialog createLoadingDialog(Context context, String msg) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_system, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
        // main.xml中的ImageView
        ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);
//        ImageView test = new ImageView(context);
        // 加载动画
        // Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
        // context, R.anim.dialog_animation2);
        // // 使用ImageView显示动画
        // spaceshipImage.startAnimation(hyperspaceJumpAnimation);
        spaceshipImage.setBackgroundResource(R.anim.dialog_animation2);
        AnimationDrawable animationDrawable = (AnimationDrawable) spaceshipImage
                .getBackground();
        // 开始执行动画
        //tipTextView.setTextColor(0xff000000);
        animationDrawable.start();
//        TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
//        if (!StringUtil.isBlank(msg)) {
//            tipTextView.setVisibility(View.VISIBLE);
//            tipTextView.setText(msg);// 设置加载信息
//        } else {
//            tipTextView.setVisibility(View.GONE);
//        }
        Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog
        // loadingDialog.setCancelable(false);// 不可以用“返回键”取消
        loadingDialog.setContentView(layout);// 设置布局
        loadingDialog.setCanceledOnTouchOutside(false);
        return loadingDialog;
    }

    public static void closeDialog(Dialog dialog) {
        if (dialog != null && dialog.isShowing()) {
            try {
                dialog.dismiss();
            } catch (Exception e) {

            }
        }
    }

    /**
     * 展示进度框
     *
     * @param context
     * @param title
     * @param message
     */
    public void openProgressDialog(Context context, String title, String message) {
        closeProgressDialog();
        progressDialog = new ProgressDialog(context);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setTitle(title);
        progressDialog.setMessage(message);
        progressDialog.show();
    }

    /**
     * 关闭进度框
     */
    public void closeProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.cancel();
            progressDialog.dismiss();
            progressDialog = null;
        }
    }
}
